log using DO_IPW_DEV , replace
/**********************/
/*Clean and set memory*/
/**********************/
clear all
set matsize 5000
set more off

/******************/
/*Install programs*/
/******************/
/*
cap ssc install xtabond2 
cap ssc install xtivreg2 
cap ssc install spmat 
cap ssc install spmack
*/
/*********************/
/*Sets base directory*/
/*********************/
global project "D:\IMF_INE_2_2021"  /* Set base directory                         */

local  repsBS=100                                                                           /* Number of bootstrap repetitions            */

/****************************/
/*Create anxillary variables*/
/****************************/

use "$project/imf_ehii_code.dta"
xtset ccode1 year
rename ehii y 

/*Drop high-income countries*/
gen high=.
replace high=1 if cname=="Andorra"
replace high=1 if cname=="Antigua and Barbuda"
replace high=1 if cname=="Aruba"
replace high=1 if cname=="Australia"
replace high=1 if cname=="Austria"
replace high=1 if cname=="Bahamas, The"
replace high=1 if cname=="Bahrain"
replace high=1 if cname=="Barbados"
replace high=1 if cname=="Belgium"
replace high=1 if cname=="Bermuda"
replace high=1 if cname=="British Virgin Islands"
replace high=1 if cname=="Brunei Darussalam"
replace high=1 if cname=="Canada"
replace high=1 if cname=="Cayman Islands"
replace high=1 if cname=="Channel Islands"
replace high=1 if cname=="Chile"
replace high=1 if cname=="Croatia"
replace high=1 if cname=="Curacao"
replace high=1 if cname=="Cyprus"
replace high=1 if cname=="Czech Republic"
replace high=1 if cname=="Denmark"
replace high=1 if cname=="Estonia"
replace high=1 if cname=="Faroe Islands"
replace high=1 if cname=="Finland"
replace high=1 if cname=="France"
replace high=1 if cname=="French Polynesia"
replace high=1 if cname=="Germany"
replace high=1 if cname=="Gibraltar"
replace high=1 if cname=="Greece"
replace high=1 if cname=="Greenland"
replace high=1 if cname=="Guam"
replace high=1 if cname=="Hong Kong SAR, China"
replace high=1 if cname=="Hungary"
replace high=1 if cname=="Iceland"
replace high=1 if cname=="Ireland"
replace high=1 if cname=="Isle of Man"
replace high=1 if cname=="Israel"
replace high=1 if cname=="Italy"
replace high=1 if cname=="Japan"
replace high=1 if cname=="Korea, Rep."
replace high=1 if cname=="Kuwait"
replace high=1 if cname=="Latvia"
replace high=1 if cname=="Liechtenstein"
replace high=1 if cname=="Lithuania"
replace high=1 if cname=="Luxembourg"
replace high=1 if cname=="Macao SAR, China"
replace high=1 if cname=="Malta"
replace high=1 if cname=="Monaco"
replace high=1 if cname=="Nauru"
replace high=1 if cname=="Netherlands"
replace high=1 if cname=="New Caledonia"
replace high=1 if cname=="New Zealand"
replace high=1 if cname=="Northern Mariana Islands"
replace high=1 if cname=="Norway"
replace high=1 if cname=="Oman"
replace high=1 if cname=="Palau"
replace high=1 if cname=="Poland"
replace high=1 if cname=="Portugal"
replace high=1 if cname=="Puerto Rico"
replace high=1 if cname=="Qatar"
replace high=1 if cname=="San Marino"
replace high=1 if cname=="Saudi Arabia"
replace high=1 if cname=="Seychelles"
replace high=1 if cname=="Singapore"
replace high=1 if cname=="Sint Maarten (Dutch part)"
replace high=1 if cname=="Slovak Republic"
replace high=1 if cname=="Slovenia"
replace high=1 if cname=="Spain"
replace high=1 if cname=="St. Kitts and Nevis"
replace high=1 if cname=="St. Martin (French part)"
replace high=1 if cname=="Sweden"
replace high=1 if cname=="Switzerland"
replace high=1 if cname=="Trinidad and Tobago"
replace high=1 if cname=="Turks and Caicos Islands"
replace high=1 if cname=="United Arab Emirates"
replace high=1 if cname=="United Kingdom"
replace high=1 if cname=="United States"
replace high=1 if cname=="Uruguay"
replace high=1 if cname=="Virgin Islands (U.S.)"

drop if high==1



/* Identifies transitions */
gen timfprogram=.
replace timfprogram=1 if imf_all==1&l.imf_all==0
replace timfprogram=0 if imf_all==0&l.imf_all==0  

/* Defines lags of inequality */
gen lag1y=l1.y
gen lag2y=l2.y
gen lag3y=l3.y
gen lag4y=l4.y                       

/* Drop observations with no data to estimate probit */
keep if lag1y!=. 
keep if lag2y!=.
keep if lag3y!=.
keep if lag4y!=.                     

/* Creates lags of inequality */


gen ydep1=L5.y-L.y
gen ydep2=L4.y-L.y
gen ydep3=L3.y-L.y
gen ydep4=L2.y-L.y
gen ydep5=0                        

/* Creates leads of inequality */
gen ydep6=y-L.y
gen ydep7=F1.y-L.y
gen ydep8=F2.y-L.y
gen ydep9=F3.y-L.y
gen ydep10=F4.y-L.y
gen ydep11=F5.y-L.y
  gen ydep12=F6.y-L.y
  gen ydep13=F7.y-L.y
  gen ydep14=F8.y-L.y
  gen ydep15=F9.y-L.y

/* Define sample */
keep if timfprogram!=.	

/* Set seed */				 
set seed 12345                       



/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************ DEFINE PROGRAM THAT COMPUTES THE INVERSE PROPENSITY SCORE REWEIGHTING ESTIMATOR FOR ALL YEARS ********************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
capture program tefpas_ipw, eclass

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep1!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

/* Estimates for year -5 */
cap: teffects ipw (ydep1) (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.timfprogram]
}
else{
matrix b=(.)
}

quietly: drop dumyears* temp mtemp

/* Estimates for year -4 to -2*/
forvalues s=2(1)4{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep`s'!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

cap: teffects ipw (ydep`s') (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears* temp mtemp
}

/* Estimates for year -1 is zero by construction */
matrix b=(b, 0)

/* Estimates for year 0 to 10 */
forvalues s=6(1)15{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: gen temp=timfprogram if ydep`s'!=. &timfprogram!=.
quietly: bysort year: egen mtemp=max(temp)
quietly: tab year if mtemp==1, gen(dumyears)

cap: teffects ipw (ydep`s') (timfprogram lag1y lag2y lag3y lag4y dumyears*, noconstant probit), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears* temp mtemp
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end



/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************************************ ESTIMATION PROCEDURES AND STORING RESULTS ************************************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/




/* 2. Bootstrap and save results */ 
xtset, clear
local  repsBS=100
bootstrap _b, reps(`repsBS') cluster(ccode1): tefpas_ipw
parmest, format(estimate min95 max95) saving("$project\results_DEV\impulse_ipw_alt", replace)

/* Results, 5-1 years before transition */
estimates store e0
nlcom (effect0: (_b[c1]+_b[c2]+_b[c3]+_b[c4]+_b[c5])/5), post
estimates store e1
/* Results, 1-5 years after transition */
estimates restore e0
nlcom (effect0: (_b[c6]+_b[c7]+_b[c8]+_b[c9]+_b[c10])/5), post
estimates store e2

/* Results, 6-11 years after transition */
estimates restore e0
nlcom (effect0: (_b[c11]+_b[c12]+_b[c13]+_b[c14]+_b[c15])/5), post
estimates store e3

log close

 translate DO_IPW_DEV.smcl DO_IPW_DEV.pdf




